

/*
regressions of auction shocks on various measures of demand at auctions

output:
- t1c_*.tex: tables with various regressions; combined with python
- f1c_binscatter_b2c_shock_by_term.pdf: figure of shocks and b2c

*/


* plotting options
set scheme lean_uncluttered


********************************************************************************
********************************************************************************

use ../data/auction_notesbonds_govpx_shock.dta, clear

* b2c/fraction regressions
n di "auction demand regressions"

* for newey-west SEs
sort release_time
gen _T = _n
tsset _T


* bid-to-cover and fraction by bidder type
* for fractions: leaveout group is dealer
local b2c_types b2c_indirectBidder b2c_directBidder b2c_primaryDealer
local D_b2c_types D_b2c_indirectBidder D_b2c_directBidder D_b2c_primaryDealer
local frac_types frac_investmentFunds frac_foreign frac_misc frac_dealers
local D_frac_types D_frac_investmentFunds D_frac_foreign D_frac_misc D_frac_dealers

* labels
label var b2c_all "Bid-to-Cover"
label var b2c_all_resid "Residual"
label var D_b2c_all "Bid-to-Cover"

label var b2c_indirectBidder "Indirect"
label var b2c_directBidder "Direct"
label var b2c_primaryDealer "Primary"
label var D_b2c_indirectBidder "Indirect"
label var D_b2c_directBidder "Direct"
label var D_b2c_primaryDealer "Primary"


label var frac_dealers "Dealers"
label var frac_investmentFunds "Invest. Fund"
label var frac_foreign "Foreign"
label var frac_misc "Misc."
label var D_frac_dealers "Dealers"
label var D_frac_investmentFunds "Invest. Fund"
label var D_frac_foreign "Foreign"
label var D_frac_misc "Misc."

* for column titles
local col_titles "\( D_t^{(2Y)} \)" "\( D_t^{(3Y)} \)" "\( D_t^{(5Y)} \)" ///
	"\( D_t^{(7Y)} \)" "\( D_t^{(10Y)} \)" "\( D_t^{(30Y)} \)" ///
	"Pool \( D_t\)" 
* options for saving tex file
local tex_tab_opts label b(2) se(2) noomitted ///
	starlevels( * 0.10 ** 0.05 *** 0.01) ///
	compress nogaps nonotes substitute(\_ _) mtitles("`col_titles'") ///
	stat(N r2 , labels("Obs." "\( R^{2}\)") fmt(0 2 0 ) )




********************************************************************************
********************************************************************************
* regressions
eststo clear

* b2c regression (note: include term FEs)
eststo r0_b2c: ivreg2 D_shock D_b2c_all ibn.term_group, r bw(9)

* b2c by bidder type
eststo r0_b2c_types: ivreg2 D_shock `D_b2c_types' ibn.term_group, r bw(9)

* fractions by bidder type
eststo r0_frac_types: ivreg2 D_shock `D_frac_types' ibn.term_group, r bw(9)

n di "by term"
levelsof term_group, local(mats)
foreach m of local mats {
	eststo rt`m'_b2c: ivreg2 D_shock D_b2c_all if term_group==`m', r bw(9)
	eststo rt`m'_b2c_types: ivreg2 D_shock `D_b2c_types' if term_group==`m', r bw(9)
	eststo rt`m'_frac_types: ivreg2 D_shock `D_frac_types' if term_group==`m', r bw(9)

}

* save to tex file
* output to tex file
esttab rt*_b2c r0_b2c ///
	using ../tables/t1c_auc_demand_b2c.tex, replace ///
	`tex_tab_opts' keep(D_b2c_*)
esttab rt*_b2c_types r0_b2c_types ///
	using ../tables/t1c_auc_demand_b2c_types.tex, replace ///
	`tex_tab_opts' keep(D_b2c_*)
esttab rt*_frac_types r0_frac_types ///
	using ../tables/t1c_auc_demand_frac_types.tex, replace ///
	`tex_tab_opts' keep(D_frac_*)


********************************************************************************
********************************************************************************
* plots

n di "auction demand scatter plots"

* regressions in levels, diffs, and AR(4)
foreach reg_type in "levels" "diffs" "lags" {
	levelsof term_group, local(mats)
	foreach m of local mats {
		n di "`m'"
		local y_axis_str ytitle("D{sub:t}{sup:(`m'Y)} Shock")
		* only label bottom figures x-axis
		if inlist(`m', 10, 30) local x_axis_str xtitle("Bid-to-Cover")
		else local x_axis_str xtitle("")
		
		if "`reg_type'"=="levels" {
			binscatter2 D_shock b2c_all if term_group==`m', ///
				m(O) lcolors("scheme p1") mcolors("scheme p1") `x_axis_str' `y_axis_str' ///
				title("") name("b2c_`reg_type'_mat`m'", replace) 
		}
		else if "`reg_type'"=="diffs" {
			binscatter2 D_shock D_b2c_all if term_group==`m', ///
				m(O) lcolors("scheme p1") mcolors("scheme p1") `x_axis_str' `y_axis_str' ///
				title("") name("b2c_`reg_type'_mat`m'", replace) 
		}
		else if "`reg_type'"=="lags" {
			binscatter2 D_shock b2c_all_resid if term_group==`m', ///
				m(O) lcolors("scheme p1") mcolors("scheme p1") `x_axis_str' `y_axis_str' ///
				title("") name("b2c_`reg_type'_mat`m'", replace) 
		}
		
	}
	
	* combine with correct size and export
	local fname f1c_binsc_b2c_`reg_type'
	graph combine b2c_`reg_type'_mat2 b2c_`reg_type'_mat3 ///
		b2c_`reg_type'_mat5 b2c_`reg_type'_mat7 ///
		b2c_`reg_type'_mat10 b2c_`reg_type'_mat30, ///
		rows(3) imargin(tiny) xsize(5) ysize(7) name(`fname', replace)
	* add titles
	gr_edit .plotregion1.graph1.title.text.Arrpush "Panel A: 2-Year"
	gr_edit .plotregion1.graph2.title.text.Arrpush "Panel B: 3-Year"
	gr_edit .plotregion1.graph3.title.text.Arrpush "Panel C: 5-Year"
	gr_edit .plotregion1.graph4.title.text.Arrpush "Panel D: 7-Year"
	gr_edit .plotregion1.graph5.title.text.Arrpush "Panel E: 10-Year"
	gr_edit .plotregion1.graph6.title.text.Arrpush "Panel F: 30-Year"
	graph export ../figs/`fname'.pdf, replace
	graph export ../figs/`fname'.eps, replace
	
}



